import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


public class AddUserFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textFieldUserName;
	private JTextField textFieldPoints;
	private JLabel lblInfo;
	private JCheckBox chckboxRMB;
	private JCheckBox chckboxYXB;
	/**
	 * Create the frame.
	 */
	public AddUserFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("用户姓名");
		label.setFont(new Font("Dialog", Font.BOLD, 20));
		label.setBounds(41, 22, 85, 33);
		contentPane.add(label);
		
		textFieldUserName = new JTextField();
		textFieldUserName.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldUserName.setBounds(160, 22, 187, 33);
		contentPane.add(textFieldUserName);
		textFieldUserName.setColumns(10);
		
		JLabel label_1 = new JLabel("积分");
		label_1.setFont(new Font("Dialog", Font.BOLD, 20));
		label_1.setBounds(41, 78, 55, 33);
		contentPane.add(label_1);
		
		textFieldPoints = new JTextField();
		textFieldPoints.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				char keyChar = e.getKeyChar();
				if(!((keyChar >= '0' && keyChar <= '9') || keyChar == '.')){
					e.consume();
				}
			}
		});
		textFieldPoints.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldPoints.setColumns(10);
		textFieldPoints.setBounds(160, 78, 187, 33);
		contentPane.add(textFieldPoints);
		
		lblInfo = new JLabel("提示");
		lblInfo.setFont(new Font("Dialog", Font.BOLD, 20));
		lblInfo.setBounds(41, 123, 306, 43);
		contentPane.add(lblInfo);
		
		JButton buttonAddUser = new JButton("添加用户");
		buttonAddUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = textFieldUserName.getText();
				int points = Integer.valueOf(textFieldPoints.getText());
				int type;
				if(chckboxYXB.isSelected()){
					type = 0;
				}else if(chckboxRMB.isSelected()){
					type = 1;
				}else{
					JOptionPane.showMessageDialog(null,"请选择用户的类型");
					return;
				}
				if(Launcher.addUser(name,points,type)){
					lblInfo.setText("提示：添加用户" + name + "成功");
				}else{
					lblInfo.setText("提示: 用户" + name + "已存在");
				}
				textFieldUserName.setText(null);
				textFieldPoints.setText(null);
				chckboxRMB.setSelected(false);
				chckboxYXB.setSelected(false);
			}
		});
		buttonAddUser.setFont(new Font("Dialog", Font.BOLD, 20));
		buttonAddUser.setBounds(41, 192, 146, 25);
		contentPane.add(buttonAddUser);
		
		JButton buttonReturn = new JButton("返回主页");
		buttonReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				Launcher.showMainFrame();
			}
		});
		buttonReturn.setFont(new Font("Dialog", Font.BOLD, 20));
		buttonReturn.setBounds(224, 192, 146, 25);
		contentPane.add(buttonReturn);
		
		chckboxRMB = new JCheckBox("人民币");
		chckboxRMB.setBounds(355, 29, 72, 23);
		contentPane.add(chckboxRMB);
		
		chckboxYXB = new JCheckBox("游戏币");
		chckboxYXB.setBounds(355, 85, 66, 23);
		contentPane.add(chckboxYXB);
		setVisible(true);
	}
	protected void processWindowEvent(WindowEvent e){
		if(e.getID() == WindowEvent.WINDOW_CLOSING){
		}else{
			super.processWindowEvent(e);
		}
	}
}
